package com.xiaochong.bitong.job.schedule;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.ExecutorConfig;
import com.xiaochong.bitong.job.service.CurrencyService;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyDataVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.RedisKeyApp;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.bitong.job.schedule
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月13日下午7:44
 * @ModificationHistory Who When What
 */
@Component
public class CurrencyScheduled {

    private static Semaphore semaphore = new Semaphore(1);

    @Autowired
    RedisServer redisServer;

    @Autowired
    CurrencyInfoMapper currencyInfoMapper;

    @Autowired
    CurrencyService currencyService;

    /**
     *  app货币列表
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0/10 * * * * ?")
    public void getCurrencyList(){
            try {
                if(semaphore.tryAcquire(1)) {
                    LogTrace.info( "getCurrencyList", "getCurrencyList开始", System.currentTimeMillis() + "" );
                    currencyService.getCurrencyQuotation();
                    LogTrace.info( "getCurrencyList", "getCurrencyList结束", System.currentTimeMillis() + "" );
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogTrace.error("app货币列表" ,e);
            } finally {
                semaphore.release(1);
            }
    }

    /**
     *  货币3小时数据图表（每5分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 */5 * * * ?")
    public void getCurrencyOf3Hours(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getCurrencyOf3Hours" , "getCurrencyOf3Hours开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                List list=new ArrayList();
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    int interval=300000;
                    int time=10800000;
                    CurrencyDataVo currencyDataVo = currencyService.getCurrencyOfTime(currencyInfo.getId(),interval,time,currencyInfo.getCmcId(),1,3,0,0);
                    list.add( currencyDataVo );
                    if(currencyDataVo != null){
                        redisServer.set( RedisKeyApp.CYRRENCY_INDEX+1+":"+3+":"+currencyInfo.getCmcId(),JSON.toJSONString( list ));
                    }
                }
            }
            LogTrace.info("getCurrencyOf3Hours" , "getCurrencyOf3Hours结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  货币1天数据图表（每10分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="30 0/9 * * * ?")
    public void getCurrencyOfOneDay(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getCurrencyOfOneDay" , "getCurrencyOfOneDay开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                List list=new ArrayList();
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    int interval=600000;
                    int time=86400000;
                    CurrencyDataVo currencyDataVo = currencyService.getCurrencyOfTime(currencyInfo.getId(),interval,time,currencyInfo.getCmcId(),2,24,2,0);
                    list.add( currencyDataVo );
                    if(currencyDataVo != null){
                        redisServer.set( RedisKeyApp.CYRRENCY_INDEX+1+":"+24+":"+currencyInfo.getCmcId(),JSON.toJSONString( list ));
                    }
                }
            }
            LogTrace.info("getCurrencyOfOneDay" , "getCurrencyOfOneDay结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  货币7天数据图表（每1小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 25 0/1 * * ?")
    public void getCurrencyOf7Day(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getCurrencyOf7Day" , "getCurrencyOf7Day开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                List list=new ArrayList();
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    int interval=3600000;
                    int time=604800000;
                    CurrencyDataVo currencyDataVo = currencyService.getCurrencyOfTime(currencyInfo.getId(),interval,time,currencyInfo.getCmcId(),7,24,11,0);
                    list.add( currencyDataVo );
                    if(currencyDataVo != null){
                        redisServer.set( RedisKeyApp.CYRRENCY_INDEX+7+":"+24+":"+currencyInfo.getCmcId(),JSON.toJSONString( list ));
                    }
                }
            }
            LogTrace.info("getCurrencyOf7Day" , "getCurrencyOf7Day结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  货币30天数据图表（每4小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 23 0/4 * * ?")
    public void getCurrencyOf30Day(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getCurrencyOf30Day" , "getCurrencyOf30Day开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                List list=new ArrayList();
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    int interval=14400000;
                    long time=2592000000L;
                    CurrencyDataVo currencyDataVo = currencyService.getCurrencyOfTime(currencyInfo.getId(),interval,time,currencyInfo.getCmcId(),30,24,11,0);
                    list.add( currencyDataVo );
                    if(currencyDataVo != null){
                        redisServer.set( RedisKeyApp.CYRRENCY_INDEX+30+":"+24+":"+currencyInfo.getCmcId(),JSON.toJSONString( list ));
                    }
                }
            }
            LogTrace.info("getCurrencyOf30Day" , "getCurrencyOf30Day结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  货币1年数据图表（每天一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 26 0 * * ?")
    public void getCurrencyOfOneYear(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("getCurrencyOfOneYear" , "getCurrencyOfOneYear开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                List list=new ArrayList();
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    int interval=86400000;
                    long time=31536000000L;
                    CurrencyDataVo currencyDataVo = currencyService.getCurrencyOfTime(currencyInfo.getId(),interval,time,currencyInfo.getCmcId(),365,24,12,1);
                    list.add( currencyDataVo );
                    if(currencyDataVo != null){
                        redisServer.set( RedisKeyApp.CYRRENCY_INDEX+365+":"+24+":"+currencyInfo.getCmcId(),JSON.toJSONString( list ));
                    }
                }
            }
            LogTrace.info("getCurrencyOfOneYear" , "getCurrencyOfOneYear结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  24H资金流向分布
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 14 0/1 * * ?")
    public void capitalFlowDistribution24H(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("capitalFlowDistribution24H" , "capitalFlowDistribution24H开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null && StringUtils.isNotBlank( currencyInfo.getCmcId() )){
                    ApiResultVo apiResultVo = currencyService.capitalFlowDistribution24H(currencyInfo.getCmcId());
                    if(apiResultVo.getData() != null){
                        redisServer.set( RedisKeyApp.CAPITAL_INDEX+currencyInfo.getCmcId(),JSON.toJSONString( apiResultVo.getData() ));
                    }
                }
            }
            LogTrace.info("capitalFlowDistribution24H" , "capitalFlowDistribution24H结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  7日涨幅计算
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 45 0/1 * * ?")
    public void capitalStatistics(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("capitalStatistics" , "capitalStatistics开始" , System.currentTimeMillis()+"");

            CurrencyInfo currencyInfo = new CurrencyInfo();
            currencyInfo.setEnableFlag( true );
            currencyInfo.setDeleteFlag( false );
            List<CurrencyInfo> currencyInfoList = currencyInfoMapper.selectByEntityList( currencyInfo );
            for(int i=0;i<currencyInfoList.size();i++){
                currencyInfo = currencyInfoList.get( i );
                if(currencyInfo!=null){
                    ApiResultVo apiResultVo = currencyService.capitalStatistics( currencyInfo.getCmcId());
                        redisServer.set( RedisKeyApp.INCREASE7DAY_INDEX+currencyInfo.getCmcId(),JSON.toJSONString( apiResultVo.getData() ));
                }
            }
            LogTrace.info("capitalStatistics" , "capitalStatistics结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }
}
